import React, { useReducer, useContext } from "react";

// 这是我们初始的数据仓库 store
const dataSource = {
  query: {
    id: "",
    name: ""
  },
  visible: false,
  authList: [],
  finalList: [],
  actionItem: {
    id: "",
    name: "",
    describe: "",
    status: 1,
    actions: []
  },
  idEdit: true
};
const reducer = (state, action) => {
  switch (action.type) {
    case "queryId":
      return {
        ...state,
        query: { ...state.query, id: action.id }
      };
    case "queryName":
      return {
        ...state,
        query: { ...state.query, name: action.name }
      };
    case "authList":
      return {
        ...state,
        authList: action.authList
      };
    case "finalList":
      return {
        ...state,
        finalList: action.finalList
      };
    case "visible":
      return {
        ...state,
        visible: action.visible,
        isEdit: action.isEdit ? true : false
      };
    case "actionItem":
      return {
        ...state,
        actionItem: action.actionItem
      };
    case "idEdit":
      return {
        ...state,
        idEdit: action.idEdit
      };

    default:
      break;
  }
};

// 定义一个 UserContext，用于传递 store
const UserContext = React.createContext(dataSource);
export { UserContext, reducer, dataSource };
